package edu.bit.water.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.jdbc.BadSqlGrammarException;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.core.JsonProcessingException;

import edu.bit.water.controller.MetaEvaWsrcInfoController;
import edu.bit.water.dao.MetaEvaWsrcInfoDao;
import edu.bit.water.entity.Column;
import edu.bit.water.entity.MetaEvaWsrcInfo;

@Service
@Component
public class MetaEvaWsrcInfoService {

    //记得加上这个哇，这是告诉全世界，你要开始在这类里面使用日志
    protected static final Logger logger = LoggerFactory.getLogger(MetaEvaWsrcInfoController.class);

    @Autowired
    public MetaEvaWsrcInfoDao metaEvaWsrcInfoDao;
    @Autowired
    public MetaEvaWsrcInfoAssociationService metaEvaWsrcInfoAssociationService;
    @Autowired
    public MetaEvaWsrcInfoService metaEvaWsrcInfoService;
    private boolean flagPart = false;// 用来记录实体表字段(只有中英文属性)是否发生改变，true为已改变，false为改变
    private boolean flagAll = false;// 用来记录实体表字段（包含说有属性）是否发生改变，true为已改变，false为改变
    private List<MetaEvaWsrcInfo> listAll = null;
    private List<Column> listPart = null;

    /**
     * @return 返回全部字段部分属性
     */
    public List<Column> selectColumn() {
        if (listPart == null || flagPart) {
            listPart = metaEvaWsrcInfoDao.selectColumn();
            flagPart = false;
        }
        return listPart;
    }

    /**
     * @return 返回所有字段
     */
    public List<MetaEvaWsrcInfo> selectColumnAll() {
        if (listAll == null || flagAll) {
            listAll = metaEvaWsrcInfoDao.queryMetaEvaWsrcInfo();
            flagAll = false;
        }
        return listAll;
    }

    /**
     * @return 返回list
     * @throws JsonProcessingException
     */
    public ArrayList<MetaEvaWsrcInfo> queryMetaEvaWsrcInfo() throws JsonProcessingException {
        ArrayList<MetaEvaWsrcInfo> list = metaEvaWsrcInfoDao.queryMetaEvaWsrcInfo();
        return list;
    }

    /**
     * 查詢同組列數
     * @param faLevel 組號
     * @return 同組列數
     */
    public int queryColumnNumByFaLevel(int faLevel) {
        return metaEvaWsrcInfoDao.queryColumnNumByFaLevel(faLevel);
    }

    /**
     *
     * @return 成功返回1，失败返回0
     */
    public Map<String, Object> insertMetaEvaWsrcInfo(MetaEvaWsrcInfo metaEvaWsrcInfo) {
        Map<String, Object> modelMap = new HashMap<>();

        try {
            metaEvaWsrcInfoDao.insertMetaEvaWsrcInfo(metaEvaWsrcInfo);
            metaEvaWsrcInfoAssociationService.insertMetaEvaWsrcInfoAssociation(metaEvaWsrcInfo);
            modelMap.put("status", "success");
            flagPart = true;
            flagAll = true;
        }catch (DuplicateKeyException e){
            if(e.getMessage().contains("CS_NAME")) {
                modelMap.put("status", "failed");
                modelMap.put("msg", "中文名称已存在，请重新输入");
                return modelMap;
            }else if(e.getMessage().contains("EN_NAME")) {
                modelMap.put("status", "failed");
                modelMap.put("msg", "英文名称已存在，请重新输入");
                return modelMap;
            }
        }
        return modelMap;
    }

    /**
     *
     * @return 成功返回1，失败返回0
     */
    public int deleteMetaEvaWsrcInfo(List<String> enNameList) {
        try {
            for (String enName : enNameList) {
                metaEvaWsrcInfoDao.deleteMetaEvaWsrcInfo(enName);
            }
            return 1;
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
            return 0;
        }finally {
            flagPart = true;
            flagAll = true;
        }
    }
    /**
     * @param enName 字段英文名称
     * @return 成功返回1，失败返回0
     */
    public int deleteMetaEvaWsrcInfo(String enName) {
        int n = metaEvaWsrcInfoDao.deleteMetaEvaWsrcInfo(enName);
        flagPart = true;
        flagAll = true;
        return n;
    }

    public Map<String, Object> updateMetaEvaWsrcInfo(MetaEvaWsrcInfo metaEvaWsrcInfo) {
        logger.info("metaEvaWsrcInfo.getId():"+metaEvaWsrcInfo.getId());
        logger.info("metaEvaWsrcInfo.getCsName():"+metaEvaWsrcInfo.getCsName());
        logger.info("metaEvaWsrcInfo.getEnName():"+metaEvaWsrcInfo.getEnName());
        logger.info("metaEvaWsrcInfo.getoldName():"+metaEvaWsrcInfo.getOldName());
        logger.info("metaEvaWsrcInfo.getDefination():"+metaEvaWsrcInfo.getDefination());
        logger.info("metaEvaWsrcInfo.getDataType():"+metaEvaWsrcInfo.getDataType());
        logger.info("metaEvaWsrcInfo.getUnit():"+metaEvaWsrcInfo.getUnit());
        Map<String, Object> modelMap = new HashMap<>();


        try{
            metaEvaWsrcInfoDao.updateMetaEvaWsrcInfo(metaEvaWsrcInfo);
            if(!metaEvaWsrcInfo.getOldName().equals(metaEvaWsrcInfo.getEnName())){
                metaEvaWsrcInfoAssociationService.updateMetaEvaWsrcInfoAssociation(metaEvaWsrcInfo);
            }

            modelMap.put("status", "success");


        }catch (DuplicateKeyException e){
            if(e.getMessage().contains("CS_NAME")) {
                modelMap.put("status", "failed");
                modelMap.put("msg", "中文名称已存在，请重新输入");
            }else if(e.getMessage().contains("EN_NAME")) {
                modelMap.put("status", "failed");
                modelMap.put("msg", "英文名称已存在，请重新输入");
            }
        }



        return modelMap;
    }

}
